package com.sjhy.plugin.api;

import com.alibaba.fastjson.JSON;
import com.intellij.openapi.ui.Messages;
import com.sjhy.plugin.config.Settings;
import com.sjhy.plugin.config.UserInfo;
import com.sjhy.plugin.constants.ConfigValue;
import com.sjhy.plugin.constants.MsgValue;
import com.sjhy.plugin.dto.ProjectDto;
import com.sjhy.plugin.dto.TypeMapperGroupListDto;
import com.sjhy.plugin.tool.HttpUtils;

import javax.swing.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author guoqing
 * @date 2021/6/13 20:40
 */
public class EasyCodeBambooApi {


    /**
     * 登录
     *
     * @param settings
     * @param usernameField
     * @param passwordField
     * @param serverAddressTextField
     */
    public static boolean login(Settings settings,
                                JTextField usernameField,
                                JTextField projectCodeField,
                                JPasswordField passwordField,
                                JTextField serverAddressTextField,
                                JLabel authorLabel, JLabel loginMassageLable) {
        if (usernameField.getText() == null || "".equals(usernameField.getText().trim())) {
            loginMassageLable.setText("请输入用户名");
            return false;
        }
        settings.getUserInfo().setUsername(usernameField.getText());
        if (passwordField.getText() == null || "".equals(passwordField.getText().trim())) {
            loginMassageLable.setText("请输入密码");
            return false;
        }
        settings.getUserInfo().setPassword(passwordField.getText());
        if (serverAddressTextField.getText() == null || "".equals(serverAddressTextField.getText().trim())) {
            loginMassageLable.setText("请输入服务器地址");
            return false;
        }
        settings.getUserInfo().setServerAddress(serverAddressTextField.getText());
        if (projectCodeField.getText() == null || "".equals(projectCodeField.getText().trim())) {
            loginMassageLable.setText("请输入项目编号，新项目请输入new");
            return false;
        }
        settings.getUserInfo().setProjectCode(projectCodeField.getText());
        Map<String, Object> param = new HashMap<>();
        param.put("username", usernameField.getText().trim());
        param.put("password", passwordField.getPassword().toString().trim());
        param.put("projectCode", projectCodeField.getText().trim());
        final String result = HttpUtils.postJson(serverAddressTextField.getText(), ConfigValue.DEFAULT_LOGIN, param);
        if (result != null) {
            final UserInfo userInfo = JSON.parseObject(result, UserInfo.class);
            settings.getUserInfo().setAuthor(userInfo.getAuthor());
            settings.getUserInfo().setNickName(userInfo.getNickName());
            settings.getUserInfo().setToken(userInfo.getToken());
            settings.getUserInfo().setProjectCode(userInfo.getProjectCode());
            authorLabel.setText(settings.getUserInfo().getAuthor());
            projectCodeField.setText(userInfo.getProjectCode());
            loginMassageLable.setText("登陆成功");
            return true;
        } else {
            loginMassageLable.setText("登陆失败");
            return false;
        }
    }

    public static TypeMapperGroupListDto getCurrentProject(String currentTypeMapperGroupid) {

        Map<String, Object> param = new HashMap<>();
        param.put("projectCode", HttpUtils.getProjectCode());
        param.put("currentTypeMapperGroupid", currentTypeMapperGroupid);
        String result = HttpUtils.postJson(ConfigValue.DEFAULT_TYPE_MAPPER, param);
        if (result != null) {
            final TypeMapperGroupListDto typeMapperGroupListDto = JSON.parseObject(result, TypeMapperGroupListDto.class);
            return typeMapperGroupListDto == null ? new TypeMapperGroupListDto() : typeMapperGroupListDto;
        } else {
            return null;
        }
    }


}
